{VERSION 6 0 "IBM INTEL NT" "6.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 1 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 256 "" 1 16 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 262 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 263 "" 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 265 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 266 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 267 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 268 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 270 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 271 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 272 "" 0 1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 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 355 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 356 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 357 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 358 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 359 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 360 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 361 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 362 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 363 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 364 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 365 "Times" 1 12 0 0 0 1 2 0 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 366 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 367 "Times" 1 12 0 0 0 1 2 0 2 2 2 2 0 0 0 1 } {CSTYLE "" -1 368 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE " " -1 369 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 370 " Times" 1 12 0 0 0 1 1 2 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 371 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" 18 372 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 373 "Times" 1 12 0 0 0 1 1 2 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 374 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE " " -1 375 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 376 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 377 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 378 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 379 "Times" 1 12 0 0 0 1 2 2 1 2 2 2 0 0 0 1 } {CSTYLE "" -1 380 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 381 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 382 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 383 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 384 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 385 "Times" 1 12 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE " " -1 386 "Times" 1 12 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE "" -1 387 " Times" 1 12 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE "" -1 388 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 389 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 390 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 391 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 392 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 393 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 394 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 395 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 396 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE " " -1 397 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 398 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 399 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 400 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 401 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 402 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 403 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 404 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 405 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 406 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 407 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 1" -1 3 1 {CSTYLE "" -1 -1 "Times " 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 1 0 1 0 2 2 0 1 } {PSTYLE "Normal" -1 256 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 }3 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Normal" -1 257 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }} {SECT 0 {EXCHG {PARA 256 "" 0 "" {TEXT 256 9 "Lecture 3" }{TEXT -1 0 " " }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 52 "If ... then ... else if ... \+ then ... else ... end if" }}{PARA 0 "" 0 "" {TEXT -1 48 "In the last l ecture we studied the basics of an " }{TEXT 257 9 "If...Then" }{TEXT -1 181 " statement. We saw that this structure evaluates a Boolean (t rue or false) statements and then performed some action if a condition was met. Let's revisit one of these programs - " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 73 "x:=5;\nif type(x,integer)=true then\n print( \"This is an integer\");\nend if;" }}}{PARA 0 "" 0 "" {TEXT -1 117 "Si nce we put in an integer, the program gave us the response we wanted. \+ But what happened when we did the following:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "x:=2.2;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "if type(x,integer)=true then\n print(\"This is an integer\");\n end if;" }}}{PARA 0 "" 0 "" {TEXT -1 176 "Nothing happens! Why? We on ly told the program what to do if x was an integer. It doesn't know w hat to do under any other condition. Let's revisit the basic structur e of an " }{TEXT 258 9 "If...Then" }{TEXT -1 45 " statement to see how to modify this program." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 16 "Essentially, an " }{TEXT 259 9 "If...Then" }{TEXT -1 26 " has this basic structure:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 261 2 "if" }{TEXT -1 1 " " } {TEXT 263 18 "Boolean expression" }{TEXT -1 1 " " }{TEXT 262 4 "then" }{TEXT -1 7 " \n " }{TEXT 264 23 "sequence of statements\n" } {TEXT 265 7 "end if;" }{TEXT 260 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 151 "What if we want this to do several dif ferent things depending on the input? If there are only two condition s that can be met, then use this structure:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 266 2 "if" }{TEXT -1 21 " Boolean expr ession " }{TEXT 267 4 "then" }{TEXT -1 27 " \n sequence of statement s\n" }{TEXT 268 4 "else" }}{PARA 0 "" 0 "" {TEXT -1 24 " sequence of s tatements\n" }{TEXT 269 7 "end if;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 78 "If you have more than two conditions that can be met, then use this structure:" }}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "" 0 "" {TEXT 270 2 "if" }{TEXT -1 24 " Boolean expressi on1 " }{TEXT 271 4 "then" }{TEXT -1 27 " \n sequence of statements\n " }{TEXT 272 7 "else if" }{TEXT -1 21 " Boolean expression2 " }{TEXT 273 4 "then" }}{PARA 0 "" 0 "" {TEXT -1 23 " sequence of statements" } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 4 " " } {TEXT 278 6 ". . . " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 " " {TEXT 274 4 "elif" }{TEXT -1 21 " Boolean expressionN " }{TEXT 275 4 "then" }{TEXT -1 25 "\n sequence of statements\n" }{TEXT 276 4 "else " }{TEXT -1 26 "\n sequence of statements\n" }{TEXT 277 7 "end if;" } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 52 "Here's a n explanation of each of these statements: " }}{PARA 0 "" 0 "" {TEXT -1 3 "By " }{TEXT 280 18 "Boolean expression" }{TEXT -1 36 " we mean a statement that is either " }{TEXT 281 4 "true" }{TEXT -1 4 " or " } {TEXT 282 5 "false" }{TEXT -1 52 ". (Note: Maple's Boolean expressions have the value " }{TEXT 283 4 "true" }{TEXT -1 4 " or " }{TEXT 284 5 "false" }{TEXT -1 8 " and not" }{TEXT 286 5 " True" }{TEXT -1 4 " or \+ " }{TEXT 285 5 "False" }{TEXT -1 49 ".) Such statements are usually co nstructed using " }{TEXT 293 18 "<, <=, >, >=, <>. " }{TEXT -1 3 "or \+ " }{TEXT 294 1 "=" }{TEXT -1 46 " together possibly with the logical o perators " }{TEXT 279 12 "and, or, not" }{TEXT -1 2 ". " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 6 "Note " }{XPPEDIT 18 0 "x <> y;" "6#0%\"xG%\"yG" }{TEXT -1 32 " is denoted by x <> y in Map le. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 73 "O ther types of Boolean expressions are formed by procedures which retur n " }{TEXT 287 4 "true" }{TEXT -1 4 " or " }{TEXT 288 5 "false" } {TEXT -1 17 ". And example is " }{TEXT 289 10 "isprime(n)" }{TEXT -1 43 ". For specific values of n, this is either " }{TEXT 290 4 "true" } {TEXT -1 4 " or " }{TEXT 291 5 "false" }{TEXT -1 3 ". \n" }}{PARA 0 " " 0 "" {TEXT -1 55 "One can test a Boolean expression by using the com mand " }{TEXT 292 5 "evalb" }{TEXT -1 29 " as in the following example s" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "5<9;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "evalb(5<9);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "evalb(9<5);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "evalb(z \+ " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "if t > 1 then \n x:=3; \nend if;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 34 "The expression t > 1 is called a " }{TEXT 299 18 "Boolean expression" }{TEXT -1 221 ". The problem \+ here is that Maple does not know the value of t so it cannot tell if t > 1 is true or false. This can be fixed by putting in a value of t a s we now do. Note that a print statement is not needed if we use \"" } {TEXT 302 7 "end if;" }{TEXT -1 2 "\"," }{TEXT 301 1 " " }{TEXT -1 16 " as opposed to \"" }{TEXT 300 7 "end if:" }{TEXT -1 2 "\"." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 55 "Try to anticipa te the output of each of these programs:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 48 "t:=3:\nif t > 1 then \n x:=2;\n print(x);\nend if: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "t:=3:\nif t > 1 then \n x:=2;\nend if;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 38 "t:=0:\n if t > 1 then \n x:=2; \nend if;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 43 "t:=0:\nif t > 1 then \n x:=2; \nend if;\nx:=3;" }}} {PARA 0 "" 0 "" {TEXT -1 159 "This last program will only be useful if you know that t>1 is true. Change the value of t to 2 and see what ha ppens. Is this what you wanted to program to do?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 48 "All of these can be combi ned into the following:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 50 "t :=0:\nif t > 1 then \n x:=2; \nelse\n x:=3;\nend if;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 58 "Let's modify the pro gram at the beginning of this section:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 98 "x:=2:\nif type(x,integer)=true then\n \"This is an i nteger\";\nelse\n \"This is not an integer\";\nend if;" }}}{PARA 0 "" 0 "" {TEXT -1 10 "Note that " }{TEXT 303 4 "elif" }{TEXT -1 28 " can b e written in palce of " }{TEXT 304 7 "else if" }{TEXT -1 9 ". Also, \+ " }{TEXT 305 7 "end if " }{TEXT -1 20 " can be replaced by " }{TEXT 306 2 "fi" }{TEXT -1 46 ". Next, let's see a procedure that uses the \+ " }{TEXT 307 43 "if .. then .. elif .. then else... end if " }{TEXT 308 12 "construction" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 182 "f:= proc(y)\n if y <= 1 then \n return 1;\n elif y <= 2 then \n return 2;\n elif y <= 3 then \n return 3;\n elif y <= 4 then \n return 4 ;\n else \n return infinity;\n end if;\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "f(-4);f(1);f(2);f(3);f(4);f(5);" }}} {PARA 0 "" 0 "" {TEXT -1 174 "Note that a procedure returns only one v alue. When a value is returned the rest of the procedure is skipped.\n The same result can be accomplished with the following procedure:" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 166 "g:=proc(y)\nif y <= 1 then \+ return 1; end if;\nif y <= 2 then return 2; end if;\nif y <= 3 then re turn 3; end if;\nif y <= 4 then return 4; end if; \nreturn infinity; \+ \nend:" }{TEXT -1 12 "or end proc:" }}}{PARA 0 "" 0 "" {TEXT -1 243 "L et's check a few numbers to make sure that these two function are, in \+ fact, identical. We could do this manually, or we could write a progr am to do it for us. Here we use a for loop, incrementing the counter \+ by .5 instead of the standard 1." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 102 "for x from 0 to 5 by .5 do \nprint(`If x= `, x, ` then f(x ) = ` , f(x), ` g(x) = `, g(x)); \nend do; " }}}{PARA 0 "" 0 "" {TEXT -1 154 "Let's practice plotting on this f(t). Note that the graph wil l be a step function. I give you two ways to plot this. One does not work, the other does." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "pl ot(f(t),t=0..5, 0..5);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "p lot(f,t=0..5, 0..5);" }}}{PARA 0 "" 0 "" {TEXT -1 84 "Here are a few m ore examples. Let's create a Boolean valued function which we call " }{TEXT 309 14 "is_increasing " }{TEXT -1 59 "that will determine if a \+ set of three number is increasing." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 100 "is_increasing:=proc(a,b,c)\nif a \+ " 0 "" {MPLTEXT 1 0 65 "is_increasing(1,2,3);\nis_increasing(1,2,1);\n is_increasing(1,1,1);" }}}{PARA 0 "" 0 "" {TEXT -1 62 "Do you remember how to get all of these to output on one line?" }}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 39 "You should kn ow by now that a number is" }{TEXT 315 6 " prime" }{TEXT -1 57 " if th e only number that divides evenly into it is 1 and " }{TEXT 316 9 "com posite" }{TEXT -1 73 " if it has divisors other than 1. Maple has a b uilt-in procedure called " }{TEXT 317 10 "isprime(n)" }{TEXT -1 49 " t hat evaluates whether n is a prime and returns " }{TEXT 318 4 "true" } {TEXT -1 3 "or " }{TEXT 319 5 "false" }{TEXT -1 29 ". Here's a proced ure called " }{TEXT 320 12 "is_composite" }{TEXT -1 3 ". " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 133 "is_composite:=proc(n)\nif n<2 then \n return false:\nelse if isprime(n)=false then\n return true:\nels e\n return true:\nend if:\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 50 "is_composite(1),is_composite(11),is_composite(10);" } }}{PARA 0 "" 0 "" {TEXT -1 24 "Note that the statement " }{TEXT 321 16 "isprime(n)=false" }{TEXT -1 34 " may be replaced by the statement \+ " }{TEXT 322 15 "not(isprime(n))" }{TEXT -1 49 " in fact the procedure may be written as follows:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 96 "is_composite2:=proc(n)\nif n < 2 then \n return false; \nend if; \nreturn not(isprime(n));\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 53 "For ... wh ile .. do ... by .. end do, next and break" }}{PARA 0 "" 0 "" {TEXT -1 28 "We have seen that the basic " }{TEXT 323 9 "If...Then" }{TEXT -1 81 " can be modified to allow for more complex programming. The sa me is true of the " }{TEXT 324 8 "For...do" }{TEXT -1 140 " loops. Th e loops can be changed to allow for more or fewer conditions restricti ng how the loop will execute. Let's look at some examples:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 62 "for i from 1 to 3 do\n y:=i^2;\n \+ sin(y*z);\n evalf(%);\nend do;" }}}{PARA 0 "" 0 "" {TEXT -1 49 "Note \+ that this could also be written without the " }{MPLTEXT 1 0 6 "from 1 " }{TEXT -1 19 " in the first line." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 55 "for i to 3 do\n y:=i^2;\n sin(y*z);\n evalf(%);\ne nd do;" }}}{PARA 0 "" 0 "" {TEXT -1 33 "As we have mentioned before, t he " }{TEXT 325 5 "index" }{TEXT -1 57 ", or counter, can be increment ed by numbers other than 1." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 83 "for j from evalf(-Pi) to evalf(Pi) by evalf(Pi/2) do\n print(evalf (sin(j)));\nend do:" }}}{PARA 0 "" 0 "" {TEXT -1 28 "We also can count backwards:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 40 "for i from 10 to 0 by -2 do\n i;\nend do;" }}}{PARA 0 "" 0 "" {TEXT -1 67 "One of \+ the greatest dangers when programming with loops are called " }{TEXT 326 14 "infinite loops" }{TEXT -1 210 ". These occur when the loop ne ver ends. This could happen for several reasons. It may be that you \+ failed to give an ending number to the counter. There may be no condi tion that causes the loop to execute a " }{TEXT 327 6 "return" }{TEXT -1 96 ", which is a line of code that ends the loop and returns some v alue to the user. Maple has the " }{TEXT 328 4 "Stop" }{TEXT -1 395 " sign up at the top of the screen to guard against infinite loops. Ho wever, it does not always work, so don't rely too heavily on it. A go od programmer tests the code piece by piece to make sure the parts wor k individually before executing them together as a program. Here are \+ some examples of infinite loops. I'm not putting them as executable c ode because I don't want you to execute them." }}{PARA 257 "" 0 "" {TEXT -1 45 "> for i from 1 do\n print (i);\n end do;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 329 86 "> for j from 1 do\n if j^2 > infinity then\n return j;\n \+ end if;\n end do;" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 297 "You might wonder why we even consider \+ loops where the index does not have a definite ending point. Problems may arise in which you do not know initially how long the computer wi ll need to execute the loop before arriving at the necessary condition or solution to the problem. Here's an example: " }}{PARA 0 "" 0 "" {TEXT 333 48 "Find the smallest number n such that n^4 > 1000." } {TEXT -1 80 " There are a few ways we can write this program. The fir st involves the use of " }{TEXT 330 5 "break" }{TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 66 "for n from 1 do\n if n^4>1000 then \n break;\n end if;\nend do:\nn;" }}}{PARA 0 "" 0 "" {TEXT -1 6 "U sing " }{TEXT 331 5 "break" }{TEXT -1 124 " caused the loop to termina te once the condition n^3 > 1000 was met. Notice that no part of the \+ loop is executed after the " }{TEXT 332 5 "break" }{TEXT -1 85 " is ca lled. The index does not increment any more, and the entire loop is s hut down." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 38 "This program can also be written with " }{TEXT 337 9 "return. " } {TEXT -1 87 "Return stops the for loop and sends the user some particu lar value or statement. NOTE:" }{TEXT 350 7 " return" }{TEXT -1 34 " \+ can only be used with procedures:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 97 "f:=proc()\nlocal n:\nfor n from 1 do\n if n^4 > 1000 then\n return n;\n end if;\nend do;\nend proc:" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "f();" }}}{PARA 0 "" 0 "" {TEXT -1 42 "Another way t o write this program is with " }{TEXT 334 4 "next" }{TEXT -1 10 ". Un like " }{TEXT 335 6 "break " }{TEXT -1 3 "and" }{TEXT 339 7 " return" }{TEXT -1 2 ", " }{TEXT 336 4 "next" }{TEXT -1 97 " will not terminate the loop, but will cause it to skip ahead to the next iteration of th e index." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 107 "g:=proc()\nloca l n;\nfor n from 1 do \n if n^4 <= 1000 then\n next;\n end if:\n \+ return n;\nend do;\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "g();" }}}{PARA 0 "" 0 "" {TEXT -1 30 "Here are some more exampl e of " }{TEXT 338 5 "break" }{TEXT -1 2 ", " }{TEXT 340 4 "next" } {TEXT -1 6 ", and " }{TEXT 341 6 "return" }{TEXT -1 75 ". This next p rogram wiill compute the product of all primes less than 100." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 103 "P:=1:\nfor m from 1 to 100 \+ do\n if isprime(m) = false then \n next; \n end if;\n P:= P * m; \nend do:\nP;" }}}{PARA 0 "" 0 "" {TEXT -1 10 "Note that " }{TEXT 342 5 "next " }{TEXT -1 38 " means to go to the next value of the " } {TEXT 344 5 "index" }{TEXT -1 25 " - in the above case the " }{TEXT 345 5 "index" }{TEXT -1 4 " is " }{TEXT 343 1 "m" }{TEXT -1 25 " - if \+ m is not prime. If " }{TEXT 346 4 "next" }{TEXT -1 53 " is executed th en the commands in the loop following " }{TEXT 347 3 "nex" }{TEXT -1 102 "t will be skipped over. In this program, if m is prime we replace P by the current value of P times m." }}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "" 0 "" {TEXT -1 60 "The next program finds the largest pr ime between 400 and 500" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 90 "f or j from 500 to 400 by -1 do\n if isprime(j)=true then\n break; \n end if;\nend do;\n j;" }}}{PARA 0 "" 0 "" {TEXT -1 75 "In the foll owing example we seek the smallest positive integer n such that " } {XPPEDIT 18 0 "1000 <= n!;" "6#1\"%+5-%*factorialG6#%\"nG" }{TEXT -1 22 ". Note that in maple " }{TEXT 348 12 "factorial(n)" }{TEXT -1 5 " and " }{TEXT 349 2 "n!" }{TEXT -1 134 " give the same result. We put the print statements in just to see what is happening. You can remov e the print statement if you wish." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 160 "for n from 1 do \n print(n,n!);\n if 1000 <= n! th en\n print(`The answer is `, n); \n break;\n end if;\nend do; \nprint(`After the loop n has the value `, n);" }}}{PARA 0 "" 0 "" {TEXT -1 88 "Another way to write loops is to impose an additional con dition. We have seen that the " }{TEXT 351 5 "break" }{TEXT -1 121 " \+ can be used to end a loop once a condition is met. Another way to acc omplish the same thing is to impose the condition " }{TEXT 352 5 "whil e" }{TEXT -1 33 " in the first line of the loop. " }{TEXT 353 5 "Whil e" }{TEXT -1 6 " is a " }{TEXT 354 8 "Boolean " }{TEXT -1 176 " expres sion that will be evaluated after the index is iterated each time. If the Boolean expression is true, then the loop will execute. If it is false, then the loop breaks." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 64 "q:=0:\nfor n from 1 while q <= 1000 do\n q:= n!;\nend do;\nn,n- 1,q;" }}}{PARA 0 "" 0 "" {TEXT -1 206 "Here are some important things \+ to notice. First, the value of n was iterated to 8, but 7 is the answ er we are looking for because 7 is the smallest number satisfying our \+ condition. Always remember that a " }{TEXT 355 5 "while" }{TEXT -1 41 " will increment the index again, whereas " }{TEXT 356 5 "break" } {TEXT -1 26 " wil not.\nNotice that the " }{MPLTEXT 1 0 4 "q:=0" } {TEXT -1 65 " is important to this code. Watch what happens if it is \+ removed:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "restart:\nfor n \+ from 1 while q <= 1000 do\n q:= n!;\nend do;\nn,n-1,q;" }}}{PARA 0 " " 0 "" {TEXT -1 171 "We get this error message because Maple does not \+ know whether q<=1000 on the first iteration of the loop.\nHere's anoth er short but effective way to write the same program." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 40 "for n from 1 while n!<1000 do\nend do;\nn ;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 61 "Since there is no code in the loop, Maple just evaluates \+ the " }{TEXT 357 5 "while" }{TEXT -1 193 " expression for increasing v alues of n until the condition is not satisfied. Notice that in this \+ case, we do not have to consider n-1 to be the answer because of how t he condition was imposed." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 23 "In general, the use of " }{TEXT 358 5 "break" } {TEXT -1 193 " will allow for more control of the code. Be careful ab out the use of while statements. If your code fails to reach a point \+ that satisfies the condition, then the loop will become infinite. " } }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 23 "For x in L do... end do" }} {PARA 0 "" 0 "" {TEXT -1 142 "In an earlier section we studied to use \+ of sets and saw some examples of how Maple could manipulate them. Ano ther object similar to sets are " }{TEXT 359 5 "lists" }{TEXT -1 32 ". In Maple, we denote a list by " }{TEXT 360 2 "[]" }{TEXT -1 102 ". L ike sets, lists are groups of elemets. Unlike sets, a list can contai n an element more than once." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "\{1,1,2,2,3,3\};" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "[1, 1,2,2,3,3];" }}}{PARA 0 "" 0 "" {TEXT -1 394 "We said that if a set ha d no elements, we denoted it by \{\}, or the empty set. If a list has no elements, we write NULL. We'll see examples of this later.\nAnoth er difference between sets and lists is the order of the elements. In sets, order is ofter lost. Lists tend to preserve order better. If \+ order is important, use [] instead of \{\}. We will spend more time l ater on the use of lists." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 100 "Loops can be used on lists. Using the following \+ syntax, you can have the loop analyze each element " }{TEXT 361 1 "x" }{TEXT -1 11 " of a list " }{TEXT 362 1 "L" }{TEXT -1 43 " and perform some action on those elements." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 61 "for x in [1,3,X,Y, t^2+t+1, hello] do\n print(x,x^2);\nend do :" }}}{PARA 0 "" 0 "" {TEXT -1 6 "Here, " }{TEXT 363 21 "[1,2,X,Y,t^2 \+ + t + 1]" }{TEXT 18 20 " is an example of a " }{TEXT 364 4 "list" } {TEXT 365 8 ". The " }{TEXT 366 1 "x" }{TEXT 367 130 " represents ea ch element of the list. The following example shows how order can be \+ cahnged with sets but is preserved with lists:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 56 "for x in \{7,3,2,Y,G, t^2+t+1\} do\n print(x,x^ 2);\nend do:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 56 "for x in [7 ,3,2,Y,G, t^2+t+1] do\n print(x,x^2);\nend do:" }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 72 "We will spend more time in a later lecture on the use of lists and sets." }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 15 "Nested do loops" }}{PARA 0 "" 0 "" {TEXT -1 303 "We have seen examples in which loops have been placed in side of, or nested, If...Then statements. We have also seen the rever se of this, where If...Then statements were placed in For...do loops. \+ We can also nest loops inside of loops. If you have studied matrices , you should see the benefit of this." }}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "" 0 "" {TEXT -1 88 "This first example will allow us to g enerate all possible pairs of the numbers [1,2,3,4]" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 79 "for i from 1 to 4 do\n for j from 1 to 4 d o\n print([i,j]);\n end do:\nend do;" }}}{PARA 0 "" 0 "" {TEXT -1 137 "Note that if the [i,j] were change to (i,j), the brackets will be removed from the output, but the parentheses will not take their plac e." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 79 "for i from 1 to 4 do\n for j from 1 to 4 do\n print((i,j));\n end do:\nend do;" }}} {PARA 0 "" 0 "" {TEXT -1 231 "Let's say we want to generate all of the 2-elemets subets of \{1,2,3,4\}. Assume that we do not want an eleme nt or a subset to be repeated, so \{1,1\}, \{2,2\}, etc. will not be o utput, and once we have \{2,4\} we will not get \{4,2\}, etc." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 84 "for i from 1 to 4 do\n for j from i+1 to 4 do\n print(\{i,j\});\n end do;\nend do;" }}} {PARA 0 "" 0 "" {TEXT 18 126 "Note carefully that the index j goes fro m i+1 to 4 so that j is always greater than i. This is legitimate in \+ this case since " }{TEXT 368 13 "\{i,j\} = \{j,i\}" }{TEXT 18 27 " and so we may assume that " }{TEXT 369 5 "i < j" }{TEXT 18 1 "." }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 97 "We can have Malpe do more than \+ just these simple tasks of printing [i,j]. The next program will " } {TEXT 18 110 "form the set of all sums of i and j where i < j and put them in a set called S when i + j is a prime number " }{TEXT 370 3 " and" }{TEXT 18 94 " we count the number of times that i + j is a prime . For this we need a \"counter\". We call it " }{TEXT 371 5 "count" } {TEXT 18 39 ". It could be any variable. We'll set " }{TEXT 372 13 "p rintlevel:=2" }{TEXT 18 56 " so we can see more of what is happening i n the program." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 390 "printleve l:=2;\nS:=\{\}:\ncount:=0:\nfor i from 1 to 4 do\n for j from i+1 to 4 do\n print(`-----------------`);\n print(i,j);\n n:=i+j ;\n if isprime(n) then\n print(`prime found`);\n count :=count+1;\n print(`count = `,count); \n S:=S union \{n\}; \n print(`S = `,S); \n end if;\n end do;\nend do;\npri nt(`----------`);\nprint(`S = `,S, `count = `,count);" }}}{PARA 0 "" 0 "" {TEXT 18 53 "Why is count = 4, yet S only contains three elements ?" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 109 "In a later lecture, I will show you how to make this out put nicer looking by using a different print command." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 86 "We can certainly nest \+ loops more than twice. The following uses three nested loops. " } {TEXT 18 64 "Note that the print statement in the following program ha s been " }{TEXT 373 13 "commented out" }{TEXT 18 68 " by use of #. Try removing the # and seeing the difference in output" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 163 "S:=\{\}:\nfor x1 from 0 to 1 do\n for x2 \+ from 0 to 1 do\n for x3 from 0 to 1 do\n print(x1,x2,x3);\n \+ S:=S union \{ [x1,x2,x3] \};\n end do:\n end do:\nend do:\nS;" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 208 "Later, we will see another way to define and create this set. \+ Note that as the number of elements increases, so does the number of \+ loops you will have to call. This can become quite tedious for larger sets." }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 26 "Local and Global Varia bles" }}{PARA 0 "" 0 "" {TEXT -1 184 "We have seen earlier that any va riable that the procedure uses must be declared to be of some certain \+ type within the body of the procedure. If not, then an error message \+ will result:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "restart:\nf: =proc(n)\n x:=n;\n print(x);\nend proc:" }}}{PARA 0 "" 0 "" {TEXT -1 52 "Note that the error message says that x is declared " }{TEXT 374 5 "local" }{TEXT -1 251 " to this procedure. So what does it mean if a variable is local or global? Quite simply, if a variable is loc al, then it can be manipulated only by that procedure. If it is globa l, than anything in the program can alter it. Here are some examples: " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=5;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "f:=proc(n)\nlocal x;\n x:=n;\n print(x);\nend proc: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "f(3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "x;" }}}{PARA 0 "" 0 "" {TEXT -1 478 "Here \+ we initially declared the variable x to be 5. Then in the procedure, \+ we created a local variable, also called x, and set it equal to n, the number that the procedure receives. Note that f(3) will set the vari able x:=3. But outside of the procedure, when we check on x, we find \+ that it is still 5. Why? Because the x in the procedure is a \"diffe rent\" x than the one we originally declared. Watch what happens when we do this same procedure, but declare x to be global:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 5 "x:=7;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 50 " f:=proc(n)\nglobal x;\n x:=n;\n print(x);\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "f(3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "x;" }}}{PARA 0 "" 0 "" {TEXT -1 288 "Now when we check on x, we see that it no longer retained its original value of 5, but \+ has been changed by the procedure. This is the effect of declaring so mething global. In this next example, we'll create four variables out side of the procedure, and then manipulate them inside of it." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 25 "(x1,x2,y1,y2):=(1,1,1,1);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 111 "f:=proc(n)\n global x1,x2;\n local y1, y2;\n x1:=2:\n x2:=3:\n y1:=5:\n y2:=7:\n return n*x1*x2*y1*y2;\n end proc:" }}}{PARA 0 "" 0 "" {TEXT -1 42 "Let's check to see what f(1 1) will return:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "f(11);" }} }{PARA 0 "" 0 "" {TEXT -1 108 "Since we're trying to see what went on \+ inside of the procedure, this output is not very helpful. Let's use \+ " }{TEXT 375 7 "ifactor" }{TEXT -1 23 " to factor this number." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "ifactor(%);" }}}{PARA 0 "" 0 "" {TEXT -1 73 "Now let's find out what's happened to the original v ariables x1,x2,y1,y2." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "x1, x2,y1,y2;" }}}{PARA 0 "" 0 "" {TEXT -1 79 "Notice that y1 and y2 are s till 1, but x1 and x2 were changed by the procedure." }}{PARA 0 "" 0 " " {TEXT -1 335 "Clearly global variables can be manipulated anywhere, \+ but local variables can only be acted upon by the procedure. I recomm end caution when you use global variables in your programs. Local var iables allow you to compartmentalize what the program can effect; glob al variables allow any part of the program to manipulate the variables ." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 78 "Exec ute the next two examples in order. Try to guess what the output will be." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 40 "f2:=proc()\nglobal x ;\n x:=2*x;\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 40 " f3:=proc()\nglobal x;\n x:=3*x;\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=4;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 " f2();" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "f3();" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 40 "Print and Return: What's the difference?" }}{PARA 0 "" 0 "" {TEXT -1 175 "We've probably noticed by now that on some instances I'l l use the print command, and on otheres I'll use return. There is a d istinct difference between these two functions. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 38 "As we have stated earlier , return can " }{TEXT 376 4 "only" }{TEXT -1 228 " be used in procedur es. If you try to use it outside of procedures, then you will get an \+ error message saying you've used return out of context. Return will e nd a procedure instantly and display whatever you tell it to return." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 240 "Print, on the other hand, can be used anywhere. It is not specific to loops , precedures, etc. Also, print does not terminate any function or pro cedure. It simply prints what you tell it to print and then goes on t o the next line of code." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT 18 50 "Consider carefully the following procedures named \+ " }{TEXT 377 6 "gprint" }{TEXT 18 5 " and " }{TEXT 378 7 "greturn" } {TEXT 18 25 ": Notice that the output " }{TEXT 379 5 "seems" }{TEXT 18 59 " to be the same for each. However, see below for difference" } {TEXT -1 2 "s:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 58 "gprint:=pr oc(x)\n local y;\n y:=2*x;\n print(y);\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 59 "greturn:=proc(x)\n local y;\n y:=2*x;\n return y;\nend proc:" }}}{PARA 0 "" 0 "" {TEXT -1 27 "Now let's test this for x=3" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "gprint(3); \ngreturn(3);" }}}{PARA 0 "" 0 "" {TEXT -1 101 "Both procedures seem t o be doing what we want. However, watch what happens when we try to f ollowing:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "m:=gprint(3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "7+gprint(3);" }}}{PARA 0 "" 0 "" {TEXT -1 42 "Now let's try the same thing with greturn." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "m:=greturn(3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "7+greturn(3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 35 "In this case we got what we wanted." }}{PARA 0 "" 0 "" {TEXT -1 41 "The moral of t his section is: DO NOT USE " }{TEXT 380 5 "print" }{TEXT -1 13 " INSTE AD OF " }{TEXT 381 6 "return" }{TEXT -1 31 " WHEN CONSTRUCTING A PROC EDURE." }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 24 "Names and reserved wor ds" }}{PARA 0 "" 0 "" {TEXT -1 127 "In previous examples, we've create d procedures called f2, f3, and made variables x1,x2,x3,y1,y2,etc. Ob viously, these are the " }{TEXT 384 5 "names" }{TEXT -1 271 " that we \+ gave to these variables. In Maple, a name can be any combination of l etters followed by numbers, underscores,etc. in which the upper and lo wer case letters are considered distinct. Names cannot contain spaces unless you jump through the following extreme hoop:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "`this is the name of a variable`:=2;" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 35 "`this is the name of a varia ble`^3;" }}}{PARA 0 "" 0 "" {TEXT -1 55 "This can also be accomplished by using underscores (_):" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 34 "this_is_the_name_of_a_variable:=3;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 33 "this_is_the_name_of_a_variable^2;" }}}{PARA 0 "" 0 " " {TEXT -1 41 "However, using just spaces will not work:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 34 "this is the name of a variable:=3; " }}}{PARA 0 "" 0 "" {TEXT -1 149 "In every programming language there are specific words that are protected under that languages code and c annot be modified by you. These are called " }{TEXT 382 8 "keywords" }{TEXT -1 4 " or " }{TEXT 383 14 "reserved words" }{TEXT -1 54 ". The se words cannot be used as names of variables. " }}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 9 "?keywords" }}}{PARA 0 "" 0 "" {TEXT -1 44 "Here are the 47 protected keywords in Maple:" }}{PARA 0 "" 0 "" {TEXT -1 732 " and assuming break by \+ catch \n description do done elif \+ else \n end error export \+ fi finally \n for from \+ global if implies \n in interse ct local minus mod \n module \+ next not od option \n options \+ or proc quit read \n \+ return save stop subset then \+ \n to try union use \+ uses \n while xor " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 18 28 "Be especially wary of usi ng " }{TEXT 385 4 "stop" }{TEXT 18 2 ", " }{TEXT 386 4 "quit" }{TEXT 18 4 " or " }{TEXT 387 4 "done" }{TEXT 18 75 ". This may cause Maple t o quit and you may lose any thing you haven't saved" }{TEXT -1 2 "!!" }}{PARA 0 "" 0 "" {TEXT -1 127 "In addition to these keywords, there a re other protected names in Maple. For instance, you could not write \+ a procedure called " }{TEXT 388 6 "factor" }{TEXT -1 101 " because Map le already had a built-in procedure with that name. However, you coul d write one called " }{TEXT 389 6 "FACTOR" }{TEXT -1 142 " and it will work just fine. Also, you cannot assign values to certain letters. \+ For instance, the letter D stands for differential operator:" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "D:=3;" }}}{PARA 0 "" 0 "" {TEXT -1 45 "Also, as we have noted earlier, I stands for " }{XPPEDIT 18 0 "sqrt(-1);" "6#-%%sqrtG6#,$\"\"\"!\"\"" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "I:=4;" }}}{PARA 0 "" 0 "" {TEXT -1 23 "Here are a few example:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "in:=2;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "'in':=2;" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "'in'^4;" }}}{PARA 0 "" 0 "" {TEXT -1 39 "Notice that this does not work on quit:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "'quit':=6;" }}}{PARA 0 "" 0 "" {TEXT -1 38 "Also, names cannot begin with numbers:" }}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 9 "3x:=Pi^2;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 17 "Indexed variables" }} {PARA 0 "" 0 "" {TEXT -1 221 "Earlier, we wrote some programs the used the notation x1, x2, y1, etc. This notation is sloppy and does not c onvey what we are really trying to express: that the numbers are subsc ripts. Maple has a way of handling this." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "x[1],x[2],x[3],x[4];" }}}{PARA 0 "" 0 "" {TEXT -1 86 "Note that these variables can be manipulated just as the other variab les we have used." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "x[1]:=6; " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "x[2]:=exp(x[1]);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "x[3]:=x[1]*x[2];" }}}{PARA 0 "" 0 "" {TEXT -1 111 "Here a program that uses indexed variables to \+ count the number of times a particular digit appears in a number:" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "N:=2^100 - 1;" }}}{PARA 0 " " 0 "" {TEXT -1 77 "Lets make 10 counters for the digits 0 through 9 a nd initialize them to zero." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "for i from 0 to 9 do\n c[i]:=0;\nend do;" }}}{PARA 0 "" 0 "" {TEXT -1 16 "Recall that the " }{TEXT 390 3 "mod" }{TEXT -1 315 " func tion looks at the remainder when one number is divided by another. Th erefore, any number mod 10 will return only the last digit of that num ber. This next part of the program will use the mod function to look \+ at the last digit in the number, increment that particular counter, an d then remove that last digit." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 66 "while N>0 do\n k:=N mod 10;\n c[k]:=c[k]+1;\n N:=(N-k)/10;\ne nd do;" }}}{PARA 0 "" 0 "" {TEXT -1 40 "Let's look at what the counter s are now:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 83 "2^100-1;\nfor \+ i from 0 to 9 do \n print(`digit`,i,`appears`, c[i], `times`);\nend d o:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 21 "assigned() and anames" }}{PARA 0 "" 0 "" {TEXT -1 140 "When we make assignments, Maple obviously stores those assignm ents in memory. We can find out if a variable has been assigned by us ing the " }{TEXT 391 10 "assigned()" }{TEXT -1 9 " command:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "x:=17;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "assigned(x);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "assigned(x );" }}}{PARA 0 "" 0 "" {TEXT -1 130 "As we have noted earlier, restart will clear everything out of memory. If you only want to clear one v ariable, use the following:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=1;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "x:='x';" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "assigned(x);" }}}{PARA 0 "" 0 "" {TEXT -1 39 "This is also true if indexed variables." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "for i from 1 to 10 do\n if isprime (i) then\n P[i]:=i;\n fi;\nend do;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 75 "for i from 1 to 10 do\n if assigned(i) then\n pri nt(i);\n end if;\nend do;" }}}{PARA 0 "" 0 "" {TEXT -1 76 "If you wan t to know all of the variables that have been assigned names, use " } {TEXT 392 6 "anames" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "anames();" }}}{PARA 0 "" 0 "" {TEXT 393 10 "`isprime/w " }{TEXT 18 6 "` and " }{TEXT 394 7 "isprime" }{TEXT 18 28 " are names assigned by Maple" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 60 "Agai n, restart will clear all of the assignments from memory" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart;" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 7 "anames;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 27 "Squares and sums of square s" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 14 "The procedure " }{TEXT 406 10 "root(x,n) " }{TEXT -1 182 "when x is real or complex and n is a po sitive integer) computes the nth root of x. If x is an integer that is a perfect nth power then the nth root is returned; otherwise the powe r " }{XPPEDIT 18 0 "x^(1/n);" "6#)%\"xG*&\"\"\"F&%\"nG!\"\"" }{TEXT -1 97 " is returned unevaluated. We can tell if x is a positive integ er by use of the Boolean function " }{TEXT 407 14 "type(x,posint)" } {TEXT -1 87 " which is true if x is a positive integer and false other wise. Here are some examples:\004" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "root(8,3); " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "root(2,3); \+ " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "root(27,3);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 32 "Compare this with the following:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "27^(1/3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "simplify(%);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "type(27^(1/3),posint);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "type(root(27,3),posint);" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 11 "root(28,3);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "type(%,posint);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 11 "The co mmand" }{TEXT 400 5 " root" }{TEXT -1 100 " also works for floating po int numbers and complex numbers. Recall that I is the square root of - 1. " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "root(28.0,3);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "root(1+2.0*I,3);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 26 "We now define a procedure " }{TEXT 401 6 "is_sqr" }{TEXT -1 127 " which determines whether or not a positive in teger is a square, that is, whether or not it is the square of anoth er integer:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 110 "is_sqr:=pro c(n)\nlocal x;\nx:=type(root(n,2),posint):\nreturn x;\nend proc:\n\n#i s_sqr:=n->type(root(n,2), posint):\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "is_sqr(16);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "is_sqr(17);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 130 "Now we want to determine whether or not the positive integer n is a sum of two po sitive squares or not. If it is, we want to find " }{TEXT 395 14 "all \+ solutions " }{TEXT -1 17 "to the equations " }{XPPEDIT 18 0 "n = x^2+y ^2;" "6#/%\"nG,&*$%\"xG\"\"#\"\"\"*$%\"yGF(F)" }{TEXT -1 41 " where x \+ and y are positive integers and " }{XPPEDIT 18 0 "x <= y;" "6#1%\"xG% \"yG" }{TEXT -1 28 ". Note we may assume that " }{XPPEDIT 18 0 "x <= sqrt(n/2);" "6#1%\"xG-%%sqrtG6#*&%\"nG\"\"\"\"\"#!\"\"" }{TEXT -1 0 " " }{TEXT 396 2 " ." }{TEXT -1 38 " [Can you see why? What happens if " }{XPPEDIT 18 0 "sqrt(n/2) < x;" "6#2-%%sqrtG6#*&%\"nG\"\"\"\"\"#! \"\"%\"xG" }{TEXT -1 46 " ?] Here's one way to write such a procedure :" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 210 "sumsqrs:=proc(n)\n lo cal x, y, Sols;\n Sols:=\{\};\n for x from 1 to evalf(sqrt(n/2)) do\n \+ y:= root(n-x^2,2);\n if type(y,posint) then \n Sols:=Sols uni on \{\{x,y\}\}; \n end if;\n end do;\n return Sols;\nend proc: " }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 34 "\nWhat happens if we leave off the " }{TEXT 397 5 "evalf" }{TEXT -1 21 " in the 4-th line of " }{TEXT 398 7 "sumsqrs" }{TEXT -1 66 "? Remove it and try. Next we illustrate \+ the use of this procedure:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "sumsqrs(50);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 50 "We check thes e two answers. Note that \{5\} = \{5,5\}." }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 19 "1^2+7^2, 5^2 + 5^2;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 120 "What would happen in the definition of sumsqrs if you re placed the command \"Sols union \{\{x,y\}\};\" by \"Sols union \{x,y\} ;\"?" }{MPLTEXT 1 0 1 " " }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 46 "The n umber of elements in a set S is given by " }{TEXT 405 7 "nops(S)" } {TEXT -1 2 ". " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "nops(\{\} );\004" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "nops(\{a,b,c,d\}) ;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 70 "We use this to find the numb er of different solutions to the equation " }{XPPEDIT 18 0 "n = x^2+y^ 2" "6#/%\"nG,&*$%\"xG\"\"#\"\"\"*$%\"yGF(F)" }{TEXT -1 28 " where x an d y are integers " }{XPPEDIT 18 0 "x <= y;" "6#1%\"xG%\"yG" }{TEXT -1 14 ". For example:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "nops(sumsqrs(50));" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 36 "Let's find the smallest n for which " }{XPPEDIT 18 0 " n = x^2+y^2;" "6#/%\"nG,&*$%\"xG\"\"#\"\"\"*$%\"yGF(F)" }{TEXT -1 36 " has at least 2 different solutions:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 100 "for n from 1 do \nif nops(sumsqrs(n)) >= 2 then \n \+ print(n,sumsqrs(n));\n break; \n end if;\nend do;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 36 "Let's find the smallest n for which " } {XPPEDIT 18 0 "n = x^2+y^2;" "6#/%\"nG,&*$%\"xG\"\"#\"\"\"*$%\"yGF(F) " }{TEXT -1 36 " has at least 3 different solutions:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 102 "for n from 1 do \n if nops(sumsqrs(n)) \+ >= 3 then \n print(n,sumsqrs(n));\n break; \n end if;\nend do;" } }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "" 0 "" {TEXT -1 92 "Here's a much more efficient program \+ due to Robert Israel for the determination of integers " }{XPPEDIT 18 0 "n <= 20^2+20^2;" "6#1%\"nG,&*$\"#?\"\"#\"\"\"*$F'F(F)" }{TEXT -1 72 " that can be expressed as the sum of two squares in more than two \+ ways. " }{TEXT 402 113 "You should use this technique when solving one of the homework exercises. So don't skip trying to understand it:\n" }{TEXT -1 42 "\nNote the use of the subscripted variable " }{TEXT 403 4 "T[m]" }{TEXT -1 17 " and the command " }{TEXT 399 8 "assigned" } {TEXT -1 84 " which tells us whether or not something has already bee n assigned to the variable " }{TEXT 404 5 "T[m]." }{TEXT -1 51 " Note that this will cover all integers from 1 to " }{XPPEDIT 18 0 "1800 = \+ 30^2+30^2;" "6#/\"%+=,&*$\"#I\"\"#\"\"\"*$F'F(F)" }{TEXT -1 1 "." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 203 "restart:\nfor i from 1 to 3 0 do\n for j from 1 to i do\n m:= i^2 + j^2;\n if assi gned(T[m]) then \n T[m]:= T[m],[i,j];\n else T[m]:= [i, j]\n end if:\n end do: \n end do:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 23 "Here are some examples:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "T[1];\nT[13];\nT[50];\nT[325];" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 78 "We can get the number of solutions for, say, T[325], by the following command:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "nops([T[325]]);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 41 "Using this we see which integers up to2* " }{XPPEDIT 18 0 "30^2;" "6#*$\"#I\"\"# " }{TEXT -1 68 " can be express as a sum of square in three or more di fferent ways: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 73 "for m fro m 1 to 2*30^2 do\nif nops([T[m]]) >=3 then print(m,T[m]); fi;\nod;" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}}{MARK "10" 0 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 } {PAGENUMBERS 0 1 2 33 1 1 }